package org.example.yiyu.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.yiyu.model.User;
import org.example.yiyu.response.LoginResponse;
import org.example.yiyu.service.LoginService;
import org.example.yiyu.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/diary")
public class LoginController {

    private final LoginService loginService;

    @PostMapping("/login")
    public LoginResponse login(@RequestBody User userReceive)
    {
        log.info("用户{}登录",userReceive.getPhoneNumber());

        User user=loginService.login(userReceive.getPhoneNumber(), userReceive.getPassword());

        //如果user的电话号码不为空，返回JWT令牌和正确的结果，如果为空，则返回密码错误
        return user.getPhoneNumber()!=null?LoginResponse.success(JwtUtil.getToken(user.getId())):LoginResponse.error();
    }

    //spring.mvc.pathmatch.matching-strategy=ant_path_matcher
}
